package com.cmcc.zysoft.infocollection.controller;

import java.util.Date;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmcc.zysoft.infocollection.common.BaseController;
import com.cmcc.zysoft.infocollection.model.Headship;
import com.cmcc.zysoft.infocollection.service.HeadshipPcService;
import com.cmcc.zysoft.spring.security.util.SecurityContextUtil;
import com.starit.common.dao.support.Pagination;

/**
 * @author 喻金龙
 * <br />邮箱: yu.jinlong@ustcinfo.com
 * <br />描述: HeadshipController.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-9-7 下午2:33:01
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Controller
@RequestMapping("/pc/headship")
public class HeadshipController extends BaseController {

	private static Logger _logger = LoggerFactory.getLogger(HeadshipController.class);
	
	@Resource
	private HeadshipPcService headshipPcService;

	
	/**
	 * 跳转至岗位息首页.
	 * @return String
	 */
	@RequestMapping("/main.htm")
	public String headship() {
		_logger.debug("跳转至岗位信息首页");
		return "pc/headship/headship";
	}
	
	/**
	 * 获取所有岗位信息.
	 * @param page 页码
	 * @param rows 每页记录数
	 * @param companyId 企业ID
	 * @param keyword 搜索关键字
	 * @return Map<String, Object>
	 */
	@RequestMapping("/all.htm")
	@ResponseBody
	public Map<String, Object> getAllHeadship(int page, int rows, String companyId, String keyword) {
		_logger.debug("获取所有岗位信息");
		return this.headshipPcService.getAllHeadship(page, rows, companyId, keyword);
	}
	
	/**
	 * 跳转至岗位添加页面.
	 * @return String
	 */
	@RequestMapping("/add.htm")
	public String add() {
		_logger.debug("跳转至岗位添加页面");
		return "/pc/headship/addHeadship";
	}

	/**
	 * 添加岗位信息.
	 * @param headship 岗位对象
	 * @return String
	 */
	@RequestMapping("/save.htm")
	@ResponseBody
	@Transactional
	public String saveHeadship(Headship headship) {
		_logger.debug("添加岗位信息");
		try{
			String userId = SecurityContextUtil.getUserId();
			if(this.headshipPcService.isHeadshipNameEqual(headship.getCompanyId(), headship.getHeadshipName(), "0")) {
				return "2";
			}		
			headship.setDelFlag("0");
			headship.setStatus("0");
			headship.setHeadshipLevel("1");
			headship.setCreateMan(userId);//设置创建人
			headship.setCreateTime(new Date());
			this.headshipPcService.insertEntity(headship);
		}catch(Exception e){
			_logger.error("添加岗位信息失败");
			e.printStackTrace();
			return "0";
		}
		return "1";
	}
	
	/**
	 * 跳转至修改岗位信息页面.
	 * @param headshipId 岗位id
	 * @param modelMap modelMap
	 * @return String
	 */
	@RequestMapping("/edit.htm")
	public String update(String headshipId, ModelMap modelMap) {
		_logger.debug("跳转至修改岗位信息页面");
		Headship headship = this.headshipPcService.getHeadship(headshipId);
 	    modelMap.addAttribute("headship", headship);
		return "pc/headship/editHeadship";
	}
	
	/**
	 * 修改岗位信息.
	 * @param headship 岗位对象
	 * @return String
	 */
	@RequestMapping("/ediHeadship.htm")
	@ResponseBody
	@Transactional
	public String updateRole(Headship headship) {
		_logger.debug("修改岗位信息");		
		try {
			String userId = SecurityContextUtil.getUserId();
			Headship newHeadship = this.headshipPcService.getEntity(headship.getHeadshipId());
			boolean check = this.headshipPcService.isHeadshipNameEqual(headship.getCompanyId(), 
					headship.getHeadshipName(), headship.getHeadshipId());
			if(check) {
				return "2";
			}
			newHeadship.setHeadshipName(headship.getHeadshipName());
			newHeadship.setDescription(headship.getDescription());
			newHeadship.setModifyMan(userId);//设置修改人
			newHeadship.setModifyTime(new Date());
			this.headshipPcService.updateHeadship(newHeadship);
		} catch (Exception e) {
			_logger.error("修改岗位信息失败");
			return "0";
		}
		return "1";
	}
	
	/**
	 * 删除岗位信息.
	 * @param headshipIds 岗位信息id集合
	 * @return String
	 */
	@RequestMapping("/delHeadship.htm")
	@ResponseBody
	@Transactional
	public String deleteHeadship(String headshipIds) {
		_logger.debug("删除岗位信息");
		try {
			  String[] ids = headshipIds.split(",");
				for(int i = 0; i < ids.length; i++) {
					//逻辑删除岗位信息
					Headship delheadship=this.headshipPcService.getEntity(ids[i]);
					delheadship.setDelFlag("1");
					this.headshipPcService.updateHeadship(delheadship);
				}
		} catch (Exception e) {
			_logger.error("删除岗位信息失败");
			return "ERROR";
		}
		return "SUCCESS";
	}
	
	/**
	 * 根据查询条件获取所有岗位信息.
	 * @param headshipName 岗位名称
	 * @param page 页码
	 * @param rows 每页记录数
	 * @return Pagination<Object>
	 */
	@RequestMapping("/allByCondition.htm")
	@ResponseBody
	public Pagination<Object> getHeadshipByCondition(String headshipName, int page, int rows) {
		_logger.debug("根据查询条件获取所有岗位信息");
		String companyId = SecurityContextUtil.getCompanyId();
		return this.headshipPcService.getHeadshipByCondition(headshipName, page, rows, companyId);
	}
}